import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import loginView from '../views/loginView.vue'
import infoView from '../views/infoView.vue'
import errorView from '../views/errorView.vue'
import funcsView from '../views/funcsView.vue'
import adviceView from '../views/adviceView.vue'
import complaintView from '../views/complaintView.vue'
import leaderMsg from '../views/leaderMsg.vue'
import checkView from '../views/checkView.vue'
import videoTalk from '../views/videoTalk.vue'
import { functions } from '@vueuse/core/metadata.cjs'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path:'/login',
      name:'login',
      component:loginView
    },
    {
      path:'/info',
      name:'info',
      component:infoView
    },
    {
      path:'/error',
      name:'error',
      component:errorView
    },
    {
      path:'/events',
      name:'events',
      component:funcsView
    },
    {
      path:'/advice',
      name:'advice',
      component:adviceView
    },
    {
      path:'/complaint',
      name:'complaint',
      component:complaintView
    },
    {
      path:'/check',
      name:'check',
      component:checkView
    },
    {
      path:'/leaderMsg',
      name:'leaderMsg',
      component:leaderMsg
    },
    {
      path:'/videoTalk',
      name:'videoTalk',
      component:videoTalk
    }
  ]
})

// 使用 find 方法替代 hasRoute
router.beforeEach((to, from, next) => {
  const route = router.getRoutes().find(r => r.path === to.path);
  if (!route) {
    next('/error'); // 如果路由不存在，重定向到 /error
  } else {
    next(); // 否则继续正常的路由跳转
  }
});

export default router
